package com.towery.dbsrcmanager.controllers;

import com.towery.dbsrcmanager.mybatis.base.dao.TDatabaseDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by User on 2016/12/30.
 */
@Slf4j
@Controller
@RequestMapping(value="/base/catalog")
public class CatalogController {

    @Autowired
    TDatabaseDAO tDatabaseDAO ;

    @RequestMapping(value="/child",method= RequestMethod.POST)
    public @ResponseBody
    List<Map<String,Object>> getCatalogChildren(String parentId,String type,String databaseid,String scema){


        if("0".equals(parentId)){
            try {
                return tDatabaseDAO.getDatabaseTreeList();
            } catch (Exception e) {
               log.error("",e);
            }
        }

        if("DATABASE".equals(type)){
            try {
                return tDatabaseDAO.getScemaList(parentId);
            } catch (Exception e) {
                log.error("",e);
            }
        }

        if("SCEMA".equals(type)){
            List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();

            Map<String,Object> node1 =new HashMap<String,Object>();
            node1.put("id",parentId+"_TABLES" );
            node1.put("name","Tables" );
            node1.put("type","Tables" );
            node1.put("databaseid",databaseid );
            node1.put("scema",scema);
            nodes.add(node1);

            Map<String,Object> node2 =new HashMap<String,Object>();
            node2.put("id",parentId+"_VIEWS" );
            node2.put("name","Views" );
            node2.put("type","Views" );
            node2.put("databaseid",databaseid );
            node2.put("scema",scema);
            nodes.add(node2);


            Map<String,Object> node3 =new HashMap<String,Object>();
            node3.put("id",parentId+"_PROCEDURES" );
            node3.put("name","Procedures" );
            node3.put("type","Procedures" );
            node3.put("databaseid",databaseid );
            node3.put("scema",scema);
            nodes.add(node3);


            Map<String,Object> node4 =new HashMap<String,Object>();
            node4.put("id",parentId+"_FUNCTIONS" );
            node4.put("name","Functions" );
            node4.put("type","Functions" );
            node4.put("databaseid",databaseid );
            node4.put("scema",scema);
            nodes.add(node4);
            
            Map<String,Object> node5 =new HashMap<String,Object>();
            node5.put("id",parentId+"_TRIGGERS" );
            node5.put("name","Triggers" );
            node5.put("type","Triggers" );
            node5.put("databaseid",databaseid );
            node5.put("scema",scema);
            nodes.add(node5);

            return nodes;
        }

        if("Functions".equals(type)  || "Procedures".equals(type) || "Views".equals(type)  || "Tables".equals(type) || "Triggers".equals(type)){
            String childType =type.substring(0,type.length()-1).toUpperCase();

            try {
                return tDatabaseDAO.getObjectTreeList(databaseid , childType,scema);
            } catch (Exception e) {
                log.error("",e);
            }
        }



        return new ArrayList<Map<String,Object>>();

    }
}
